angular.module('app')
  .directive('helper', ['$rootScope', '$modal', '$compile', function ($rootScope, $modal, $compile) {
    return {
      restrict: 'A',
      link: function ($scope, iElement, iAttrs) {
        var html = '<div>' + $(iElement).html() + '</div>';

        var title = $(html).find("h1").text() || '快速指引';
        var content = $(html).find("> div").html();

        var template = 
        '<div class="modal-header">'+
        '    <h3 class="modal-title">'+title+'</h3>'+
        '</div>'+
        '<div class="modal-body modal-helper">'+
             content +
        '</div>'+
        '<div class="modal-footer">'+                  
        '    <button class="btn btn-default" ng-click="cancel()">我知道了</button>'+
        '</div>';

        var btn = $("<button class='btn btn-default pull-right'>"+title+"</button>");
        btn.click(function(){
          var modalInstance = $modal.open({
            template: template,
            controller: 'HelperModalInstanceCtrl',
            size: 'lg',
            resolve: {}
          });
        });

        $(iElement).html(btn);
      }
    };
  }]);


  app.controller('HelperModalInstanceCtrl', ['$scope', '$modalInstance', function($scope, $modalInstance) {
    $scope.ok = function () {
      $modalInstance.close($scope.selected.item);
    };

    $scope.cancel = function () {
      $modalInstance.dismiss('cancel');
    };
  }])
  ; 
